package com.ys.controller.api.outer;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentials;
import com.amazon.SellingPartnerAPIAA.AWSAuthenticationCredentialsProvider;
import com.bo.util.DateUtil;
import com.ys.config.MyConfig;
import com.ys.entity.business.TpAmazonSeller;
import com.ys.enume.RegionEnum;
import com.ys.enume.business.TpAmazonSellerEnum;
import com.ys.service.amazonorder.TpAmazonSellerReportService;
import com.ys.util.amazon.AmazonUtil;

@Controller
@RequestMapping("/api/outer/demoXDH")
public class ApiOuterDemoXDHController {

	@Autowired
	private TpAmazonSellerReportService tpAmazonSellerReportService;
	
    @RequestMapping("/test")
	@ResponseBody
	public void test() {

		if ("prouk".equals(MyConfig.getActive())) {

			Date now = new Date();
			String latestUpdateDayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -14));
			String latestUpdateDayEnd = DateUtil.getYYYY_MM_DD(now);

			Thread t = new Thread(() -> {
				addReport(RegionEnum.EUROPE.getCode(), latestUpdateDayBegin, latestUpdateDayEnd);
			});
			t.start();
		}

		if ("prous".equals(MyConfig.getActive())) {

			Date now = new Date();
			String latestUpdateDayBegin = DateUtil.getYYYY_MM_DD(DateUtil.addDays(now, -14));
			String latestUpdateDayEnd = DateUtil.getYYYY_MM_DD(now);

			Thread t = new Thread(() -> {
				addReport(RegionEnum.NORTH.getCode(), latestUpdateDayBegin, latestUpdateDayEnd);
			});
			t.start();
		}
		
	}
	
	/**
	 * 添加报告
	 * @param regionCode 区域代码，EUROPE(欧洲)，NORTH(北美)
	 * @param latestUpdateDayBegin 最新更新时间(起始)，格式如：2024-05-13
	 * @param latestUpdateDayEnd 最新更新时间(截止)，格式如：2024-05-30
	 */
	public void addReport(String regionCode, String latestUpdateDayBegin, String latestUpdateDayEnd) {
		
		Map<String, AWSAuthenticationCredentials> MAP_REGION_Credentials = AmazonUtil.getCredentials();
		AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AmazonUtil.getProvider();
		
		// 给每个店铺创建订单报告
		List<TpAmazonSeller> listTpAmazonSeller = new TpAmazonSeller()
				.setAuthState(TpAmazonSellerEnum.AuthState.YES.value)
				.setRegionCode(regionCode)
				.setStatus(1)
				.queryList();
		for(TpAmazonSeller entity : listTpAmazonSeller) {
			String sellerId = entity.getSellerId();
			tpAmazonSellerReportService.add(sellerId, latestUpdateDayBegin, latestUpdateDayEnd, MAP_REGION_Credentials, awsAuthenticationCredentialsProvider);
		}
	}
}
